<?php
namespace Admin\Controller;
use Think\Controller;
 /**
  *商品管理
  *@date 2015-4-16 10:29:57
  *@author deng
  **/
class GoodController extends AdminController{
	public $pagesize=10;

	public function addGood(){
		
		$this->display('add_good');
	}
	
	/**
	 * 商品列表
	 * @return [type] [description]
	 */
	public function goodList(){

		$count=M('goods')->count();
		$page = new \Think\Page($count,$this->pagesize);
		$page ->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%");
		$show= $page->show(); //分页显示输出
		//dump($show);
		$list=M('goods')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('_page',$show);
		$this->assign('_list',$list);
		$this->display('good_list');		
	}

	/**
	 * 商品分类
	 * @return [type] [description]
	 */
	public function goodCate(){
		$tree = D('Category')->getTree(0,'cat_id,cat_name,cat_desc,parent_id,sort_order,show_in_nav,grade');
		//dump($tree);
		$this->assign('tree',$tree);
		$this->display('good_cate');
	}

    /**
     * 显示分类树，仅支持内部调
     * @param  array $tree 分类树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){
        //C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }

	/**
	 * 添加商品分类
	 */
	public function addCate(){
		if(IS_POST){
			$data = array(
				'cat_name' => I('post.catname','','trim'),
				'keywords' => I('post.keyword','','trim'),
				'cat_desc' => I('post.desc','','trim'),
				'parent_id' => I('post.pid','','intval'),
				'sort_order' => I('post.sort','0','intval'),
				'measure_unit' => I('post.unit','','trim'),
				'show_in_nav' => I('post.isnav','0','intval'),
				'is_show' => I('post.isshow','0','intval'),
				'style' => I('post.style','','trim'),
				'grade' => I('post.grade','0','intval'), //区间个数	
				//'filter_attr=>'',
				);
			if(empty($data) || empty($data['cat_name'])){
				$this->error('提交数据错误');	
			}

			$add = M('Category')->data($data)->add();
			if($add){
				$this->success('添加分类成功');
			}else{
				$this->error('添加分类失败');
			}
		}else{
			$this->display('add_cate');
		}
	}

	/**
	 * 商品品牌列表
	 * @return [type]
	 */
	public function goodBrand(){
		$count=M('brand')->count();
		$page = new \Think\Page($count,10);
		$page ->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%");
		$show= $page->show(); //分页显示输出
		//dump($show);
		$list=M('brand')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('_page',$show);
		$this->assign('_list',$list);
		$this->display('good_brand');
	}
	

	public function addBrand(){
		if(IS_POST){
				$data=array(
					'brand_name'=>I('post.brandname','','trim'),
					'brand_logo'=>I('post.logourl','','trim'),
					'site_url'=>I('post.brandurl','','trim'),
					'brand_desc'=>I('post.branddesc','','trim'),
					'sort_order'=>I('post.sort','','trim'),
					'is_show'=>I('post.is_show','','trim'),
					);
				$add=M('brand')->data($data)->add();
				if($add){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
		}else{
			$this->display('add_brand');	
		}
		
	}

	/**
	 * 商品类型列表
	 * @return [type]
	 */
	public function goodType(){
		$list=M('goods_type')->select();
		$this->assign('_list',$list);
		$this->display('good_type');
	}
	
	/**
	 * 增加商品类型
	 * @author dch
	 */
	public function addType(){
		if(IS_POST){
			
			$goodname=I('post.goodname','','trim');
			$enable=I('post.enabled','1','intval');
			if(empty($goodname)){
				$this->error('类型不为空');
			}
			
			$add=M('goods_type')->data(array('cat_name'=>$goodname,'enable'=>$enable))->add();
			if($add){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
			
		}
		$this->display('add_type');
	}
	
	/**
	 * 商品类型属性列表
	 * @author dch
	 */
	public function typeAttribute(){
		$typeid = I('get.typeid','','intval');
		empty($typeid) and $this->error('参数错误');
		
		//类型下拉
		$typelist=M('goods_type')->select();
		$this->assign('typelist',$typelist);
		
		$count = M('attribute')->where(array('cat_id'=>$typeid))->count();
		$page = new \Think\Page($count,10);
		$page ->setConfig('theme',"%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%");
		$show= $page->show(); //分页显示输出
		//dump($show);
		$list=M('attribute')->where(array('cat_id'=>$typeid))->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('_list',$list);
		$this->assign('_page',$show);
		$this->display('type_attribute');
	}

	/**
	 * 编辑商品类型属性
	 */
	public function editAttr(){
		$typeid = I('typeid','','intval');
		empty($typeid) and $this->error('商品类型为空',U('Good/goodType'));

		if(IS_POST){
			$aid = I('post.aid','','intval');
			
			$data = array(
					'cat_id' => $typeid,
					'attr_name' => I('post.attrname','','trim'),// 类型名称
					'attr_input_type' => I('post.inputway','','intval'),// 录入的类型
					'attr_index' => I('post.issearch','','intval'), //是否进行检索
					'attr_values' => I('post.inputtext','','trim'), //录入的值
					'attr_type' =>I('post.isselect','','intval'), //属性的类型，是否可选
					'is_linked' => I('post.isjoin','','intval'),//是否关联
					);
			//@todo 需要验证数据
			if(empty($aid)){
				//新增属性
				$edit=M('attribute')->data($data)->add();

			}else{
				//编辑属性
				$edit=M('attribute')->where(array('attr_id'=>$aid))->save($data);
			}

			if($edit){
				$this->success('操作成功',U('Good/typeAttribute',array('typeid'=>$typeid)));	
			}else{
				$this->error('操作失败',U('Good/typeAttribute',array('typeid'=>$typeid)));
			}

		}else{
			$aid = I('get.aid','','intval');
			
			if(isset($_GET['aid'])){
				$info=M('attribute')->find($aid);
				empty($info) and $this->error('参数错误，该属性不存在');
				$this->assign('info',$info);
			}
			//类型下拉
			$typelist=M('goods_type')->select();
			$this->assign('typeid',$typeid);
			$this->assign('typelist',$typelist);
			$this->display('edit_attr');
		}
	}

	/**
	 * [删除属性]
	 * @return [type] [description]
	 */
	public function delAttr(){
		$aid = I('get.aid','','intval');
		empty($aid) and $this->error('参数错误');
		$del=M('attribute')->where(array('attr_id'=>$aid))->delete();
		if($del){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
	}
}